package com.xxx.test;

import com.xxx.DAO.booksDao;
import com.xxx.entity.Book;
import com.xxx.util.JDBCUtil;
import com.xxx.util.pageUtil;
import com.xxx.DAO.user_bookDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@WebServlet("/showTable2")
public class showTable2 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String search=null;
        String bType=null;
        String userId = request.getSession().getAttribute("id").toString();
        int iPageSize = 4, iTotal = 0, iPageCnt = 0, iPage = 0;
        if (request.getParameter("page") != null) {// 从翻页操作链接进入
            try {
                iPage = Integer.parseInt(request.getParameter("page"));
            } catch (RuntimeException e) {
                e.printStackTrace();
            }
        }
        if (iPage < 1) { iPage = 1; }
        request.setAttribute("iPage",iPage);
        booksDao dao=new booksDao();//创建booksDao类
        List<Book> booksList = new ArrayList<>();
        List<Book> booksList2 = new ArrayList<>();
        pageUtil pageutil=new pageUtil();//创建分页类
        JDBCUtil util=new JDBCUtil();
        if (request.getParameter("search") != null) {
            bType = new String(request.getParameter("bType").getBytes("iso-8859-1"), "UTF-8");
            search = new String(request.getParameter("search").getBytes("iso-8859-1"), "UTF-8");
            booksList = dao.select(search,bType,userId);//生成查询结果book类的list
            iTotal = pageutil.getPageCount(booksList);
            iPageCnt = (iTotal + iPageSize - 1) / iPageSize;
            if (iPage > iPageCnt) {
                iPage = iPageCnt;
            }
            if (iPageCnt > 0) {
                booksList2 = pageUtil.startPage(booksList, iPage, 4);//分页后的book类list
                request.setAttribute("list", booksList2);
                request.setAttribute("iPageCnt", iPageCnt);
            }
        }else if (request.getParameter("type")!=null){
            String type= request.getParameter("type");
            booksList=dao.selectType(type,userId);//生成查询结果book类的list
            iTotal = pageutil.getPageCount(booksList);
            iPageCnt=(iTotal+iPageSize-1)/iPageSize;
            if (iPage>iPageCnt) { iPage=iPageCnt;}
            if (iPageCnt>0){
                booksList2 = pageUtil.startPage(booksList, iPage, 4);//分页后的book类list
                request.setAttribute("list",booksList2);
                request.setAttribute("iPageCnt",iPageCnt);
            }
        }else{
            booksList=dao.findAll(userId);
            iTotal = pageutil.getPageCount(booksList);
            iPageCnt=(iTotal+iPageSize-1)/iPageSize;
            if (iPage>iPageCnt) { iPage=iPageCnt; }
            if (iPageCnt>0){
                booksList2 = pageUtil.startPage(booksList, iPage, 4);
                request.setAttribute("list",booksList2);
                request.setAttribute("iPageCnt",iPageCnt);
            }
        }
        request.getRequestDispatcher("Catalog1.jsp").forward(request,response);


    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

}

